package com.ccrfid.rmc6.service.poi;

import com.ccrfid.rmc6.entity.poi.PoiLocationEntity;
import com.ccrfid.rmc6.service.amq.MqMsgProducer;
import com.ccrfid.rmc6.util.JsonUtil;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Aspect
@Component
public class PoiLocationServiceAspect {
    private static final Logger logger = LoggerFactory.getLogger(PoiLocationServiceAspect.class);
    @Autowired
    private MqMsgProducer mqMsgProducer;

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.poi.PoiLocationService.createPoiLocation(..))",
            returning = "ret")
    public void afterCreateLocation(PoiLocationEntity ret) {
        String msg = "[" + ret.getId() + "]";
        mqMsgProducer.send2Topic("ws_location_add", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.poi.PoiLocationService.updatePoiLocation(..))",
            returning = "ret")
    public void afterUpdatePoiLocation(PoiLocationEntity ret) {
        String msg = "[" + ret.getId() + "]";
        mqMsgProducer.send2Topic("ws_location_update", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.poi.PoiLocationService.deletePoiLocations(..)) && args(idList)",
            argNames = "idList")
    public void afterDeletePoiLocations(List<Long> idList) {
        String msg = JsonUtil.INSTANCE.writeValueAsString(idList);
        mqMsgProducer.send2Topic("ws_location_delete", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.poi.PoiLocationService.bindLocationOrg(..))",
            returning = "ret")
    public void afterBindPoiLocationOrg(PoiLocationEntity ret) {
        String msg = "[" + ret.getId() + "]";
        mqMsgProducer.send2Topic("ws_location_update", msg);
    }

    @AfterReturning(value = "execution(public * com.ccrfid.rmc6.service.poi.PoiLocationService.unBindLocationOrg(..))",
            returning = "ret")
    public void afterUnBindPoiLocationOrg(PoiLocationEntity ret) {
        String msg = "[" + ret.getId() + "]";
        mqMsgProducer.send2Topic("ws_location_update", msg);
    }
}
